package controllers.customer;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.AuctionService;
import services.BidService;
import services.PaintingService;
import controllers.AbstractController;
import domain.Auction;
import domain.Bid;
import domain.Painting;


@Controller
@RequestMapping("/auction/customer")
public class AuctionCustomerController extends AbstractController{
	
	// Services
	@Autowired
	private AuctionService auctionService;
	@Autowired
	private PaintingService paintingService;
	@Autowired
	private BidService bidService;
	

	// Constructors
	public AuctionCustomerController() {
		super();
	}

	// Listing methods
	@RequestMapping(value = "/myAuctions", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Auction> auctions;
		Boolean isOwner;
		isOwner = true;
		auctions = auctionService.findAuctionsByCustomerId();

		result = new ModelAndView("auction/myAuctionsList");
		result.addObject("requestURI", "auction/customer/myAuctions.do");
		result.addObject("auctions", auctions);
		result.addObject("isOwner", isOwner);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/myFinishedAuctionsList", method = RequestMethod.GET)
	public ModelAndView myFinishedAuctionList() {
		ModelAndView result;
		Collection<Auction> auctions;
		Boolean isOwner;
		isOwner = true;
		auctions = auctionService.findFinishedAuctionsByCustomer();

		result = new ModelAndView("auction/myFinishedAuctionsList");
		result.addObject("requestURI",
				"auction/customer/myFinishedAuctionsList.do");
		result.addObject("auctions", auctions);
		result.addObject("isOwner", isOwner);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/activeAuctionsListByOther", method = RequestMethod.GET)
	public ModelAndView activeAuctionListByOther() {
		ModelAndView result;
		Collection<Auction> auctions;
		Boolean isOwner;
		Boolean hasBid;
		
		hasBid = false;
		isOwner = false;
		
		auctions = auctionService.findActiveAuctionsByOther();

		result = new ModelAndView("auction/activeList");
		result.addObject("requestURI",
				"auction/customer/activeAuctionsListByOther.do");
		result.addObject("auctions", auctions);
		result.addObject("isOwner", isOwner);
		result.addObject("hasBid", hasBid);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	// Create
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int paintingId) {
		ModelAndView result;
		Boolean isNew;
		Boolean hasBid;
		Painting painting;
		Auction auction;

		painting = paintingService.findOne(paintingId);

		auction = auctionService.create(painting);
		auction.setPainting(painting);
		
		isNew = true;
		hasBid = false;

		result = new ModelAndView("auction/create");
		result.addObject("requestURI", "auction/customer/edit.do");
		result.addObject("auction", auction);
		result.addObject("isNew", isNew);
		result.addObject("hasBid", hasBid);
		result.addObject("requestURI", "auction/customer/edit.do");

		return result;

	}
	
	// Edition
	
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int auctionId) {
		ModelAndView result;
		Boolean isNew;
		Boolean hasBid;
		Auction auction;
		Bid bid;
		int bidId;
		
		isNew = false;
		auction = auctionService.findOne(auctionId);
		bid = bidService.findBidByCustomerAndAuction(auction.getId());
		hasBid = bid!= null;
		
		result = new ModelAndView("auction/edit");
		
		if(hasBid){
			bidId = bid.getId();
			result.addObject("bidId", bidId);
		}
	
		
		result.addObject("requestURI", "auction/customer/edit.do");
		result.addObject("isNew", isNew);
		result.addObject("auction", auction);
		result.addObject("auctionId", auctionId);
		result.addObject("hasBid", hasBid);
		

		return result;

	}



	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Auction auction, BindingResult binding) {
		ModelAndView result;
		Boolean isNew;
		Boolean hasBid;

		if (binding.hasErrors()) {
			result = createEditModelAndView(auction);
		} else {
			try {
				auctionService.save(auction);
				result = new ModelAndView("redirect:../../");

			} catch (Throwable oops) {

				result = createEditModelAndView(auction, "auction.commit.error");
			}
		}
		isNew = auction.getId() == 0;
		hasBid = bidService.findBidByCustomerAndAuction(auction.getId())!= null;
	

		result.addObject("auction", auction);
		result.addObject("isNew", isNew);
		result.addObject("hasBid", hasBid);
		result.addObject("requestURI", "auction/customer/edit.do");

		return result;

	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(Auction auction) {
		ModelAndView result;

		result = createEditModelAndView(auction, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Auction auction,
			String message) {
		ModelAndView result;

		result = new ModelAndView("auction/edit");

		result.addObject("auction", auction);
		result.addObject("message", message);

		return result;
	}


}
